

package com.kimo.shop.admin.controller;

import cn.hutool.core.io.FileUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.kimo.shop.bean.enums.FileType;
import com.kimo.shop.bean.model.AttachFile;
import com.kimo.shop.common.exception.KimoShopBindException;
import com.kimo.shop.common.util.PageParam;
import com.kimo.shop.config.ShopConfig;
import com.kimo.shop.service.AttachFileService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 文件上传 controller
 * @author kimo-taozhouchuan
 *
 */
@RestController
@RequestMapping("/admin/file")
public class FileController {

	@Autowired
	private AttachFileService attachFileService;
	@Autowired
	private ShopConfig shopConfig;

	@PostMapping("/upload/element")
	@ApiOperation(value = "上传视频文件")
	public ResponseEntity<Long> uploadElementFile(@RequestParam("file") MultipartFile file) throws IOException{
		if(file.isEmpty()){
            return ResponseEntity.noContent().build();
        }
		AttachFile attachFile = new AttachFile();
		attachFile.setType(FileType.VIDEO.value());
		Long fileId = attachFileService.uploadPlatformFile(file.getBytes(),file.getOriginalFilename(),attachFile);
        return ResponseEntity.ok(fileId);
	}

	@PostMapping("/upload/tinymceEditor")
	public ResponseEntity<String> uploadTinymceEditorImages(@RequestParam("editorFile") MultipartFile editorFile) throws IOException{
		String fileName =  attachFileService.uploadFile(editorFile.getBytes(),editorFile.getOriginalFilename());
        return ResponseEntity.ok(shopConfig.getDomain().getResourcesDomainName() + "/"  + fileName);
	}

	@PostMapping("/upload/img")
	@ApiOperation(value = "上传图片文件")
	public ResponseEntity<Long> uploadImg(@RequestParam("file") MultipartFile file) throws IOException{
		if(file.isEmpty()){
			return ResponseEntity.noContent().build();
		}
		AttachFile attachFile = new AttachFile();
		String extName = FileUtil.extName(file.getOriginalFilename());
		attachFile.setFileType(extName);
		attachFile.setFileName(FileUtil.mainName(file.getOriginalFilename()));
		attachFile.setType(FileType.IMAGE.value());
		attachFile.setUploadTime(new Date());
		Long fileId = attachFileService.uploadImg(file.getBytes(),attachFile,extName);
		return ResponseEntity.ok(fileId);
	}

	/**
	 * 分页获取历史图片
	 */
	@GetMapping("/attachFilePage")
	public ResponseEntity<IPage<AttachFile>> attachFilePage(PageParam<AttachFile> page, AttachFile attachFile) {
		IPage<AttachFile> attachFilePage = attachFileService.getPage(page,attachFile);
		return ResponseEntity.ok(attachFilePage);
	}

	/**
	 * 删除图片
	 */
	@DeleteMapping("/deleteFile/{fileId}")
	public ResponseEntity<Void> deleteFile(@PathVariable("fileId") Long fileId){
		AttachFile attachFile = attachFileService.getById(fileId);
		attachFileService.deleteFile(attachFile.getFilePath());
		return ResponseEntity.ok().build();
	}

	@DeleteMapping("/deleteByIds")
	@ApiOperation(value = "根据文件id列表批量删除文件记录", notes = "根据文件id列表批量删除文件记录")
	public ResponseEntity<Void> deleteByIds(@RequestBody List<Long> ids) {
		attachFileService.deleteByIdsAndShopId(ids);
		return ResponseEntity.ok().build();
	}

	@PutMapping("/batchMove")
	@ApiOperation(value = "根据文件id列表与分组id批量移动文件", notes = "根据文件id列表与分组id批量移动文件")
	public ResponseEntity<Void> batchMove(@RequestBody List<Long> ids, @RequestParam(value = "groupId", required = false, defaultValue = "") Long groupId) {
		attachFileService.batchMoveByShopIdAndIdsAndGroupId(ids, groupId);
		return ResponseEntity.ok().build();
	}

	/**
	 * 上传聊天时的文件
	 * @param file
	 * @return
	 * @throws IOException
	 */
	@PostMapping("/upload/imFile")
	public ResponseEntity<String> uploadImFile(@RequestParam("file") MultipartFile file) throws IOException{
		if(file.isEmpty()){
			return ResponseEntity.noContent().build();
		}
		String fileName = attachFileService.uploadImFile(file.getBytes(),file.getOriginalFilename());
		return ResponseEntity.ok(fileName);
	}

	/**
	 * 更改图片名称与分组
	 */
	@PutMapping("/updateFile")
	public ResponseEntity<Boolean> updateFile(@RequestBody AttachFile attachFile) {
		if (Objects.isNull(attachFile.getFileName())){
			// 图片名称不能为空
			throw new KimoShopBindException("kimo.img.name.exist");
		}
		return ResponseEntity.ok(attachFileService.updateFile(attachFile));
	}

	@GetMapping("/get_file_by_id")
	@ApiOperation(value = "根据文件id获取文件信息")
	public ResponseEntity<AttachFile> getFileById(Long fileId){
		AttachFile attachFile = attachFileService.getById(fileId);
		return ResponseEntity.ok(attachFile);
	}

	@PostMapping("/save_pdf_file")
	@ApiOperation(value = "保存商家发票")
	@PreAuthorize("@pms.hasPermission('order:orderInvoice:upload')")
	public ResponseEntity<AttachFile> savePdfFile(@RequestParam("file") MultipartFile file) throws IOException{
		AttachFile attachFile = new AttachFile();
		attachFile.setType(FileType.FILE.value());
		Long fileId = attachFileService.uploadPlatformFile(file.getBytes(), file.getOriginalFilename(),attachFile);
		return ResponseEntity.ok(attachFileService.getById(fileId));
	}

}
